import JSEncrypt from 'jsencrypt'

const systemPublicKey = `-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMYSCg3ocg3n+N/Fz8i0x6gdSZ7wSO8d
1mr5ECEn1v/AVyuIOTehlTo4Ql715FfK/rAgHXKKu4JFgcoaUl93zxECAwEAAQ==
-----END PUBLIC KEY-----`

/**
 * 对数据进行加密
 * @param publicKey 公钥
 * @param txt 需要加密的字符串
 * @returns
 */
export function encrypt(txt: string, publicKey: string = systemPublicKey) {
  const encryptor = new JSEncrypt()
  encryptor.setPublicKey(publicKey)
  return encryptor.encrypt(txt) as string
}

/**
 *
 * @param privateKey 密钥
 * @param txt 需要解密的字符串
 * @returns
 */
export function decrypt(txt: string, privateKey: string) {
  const encryptor = new JSEncrypt()
  encryptor.setPrivateKey(privateKey)
  return encryptor.decrypt(txt)
}
